<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>FreeMarker-XML处理笔记 | 净土</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="FreeMarker;Java模板引擎;XML处理">
<meta property="og:type" content="article">
<meta property="og:title" content="FreeMarker-XML处理笔记">
<meta property="og:url" content="http://howiefh.github.io/2015/05/03/freemarker-XML-processing-note/index.html">
<meta property="og:site_name" content="净土">
<meta property="og:description" content="FreeMarker;Java模板引擎;XML处理">
<meta property="og:updated_time" content="2015-05-21T15:55:14.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="FreeMarker-XML处理笔记">
<meta name="twitter:description" content="FreeMarker;Java模板引擎;XML处理">
  
    <link rel="alternative" href="/atom.xml" title="净土" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.ico">
  
  <link href="http://fonts.useso.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="/css/style.css" type="text/css">
  
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-40492061-1', 'auto');
ga('send', 'pageview');

</script>


  
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?56d2899c5e919fbf4a7b00de5d1c31dd";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>


</head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">净土</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">乐不在外而在心，心以为乐，则是境皆乐；心以为苦，则无境不苦。</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-github-link" class="nav-icon" href="https://github.com/howiefh" title="Github" target="_blank"></a>
        
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed" target="_blank"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://howiefh.github.io"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-freemarker-XML-processing-note" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    
<a href="/2015/05/03/freemarker-XML-processing-note/" class="article-date">
  <time datetime="2015-05-03T01:20:51.000Z" itemprop="datePublished">2015-05-03</time>
</a>


    
  <div class="article-category">
    <a class="article-category-link" href="/categories/Java/">Java</a>►<a class="article-category-link" href="/categories/Java/FreeMarker/">FreeMarker</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      FreeMarker-XML处理笔记
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
		
		<div id="toc" class="toc-article">
			<h2 class="toc-title"><span>Contents</span></h2>
		
			<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#揭示XML文档"><span class="toc-number">1.</span> <span class="toc-text">揭示XML文档</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#将XML放到数据模型中"><span class="toc-number">1.1.</span> <span class="toc-text">将XML放到数据模型中</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#必要的XML处理"><span class="toc-number">2.</span> <span class="toc-text">必要的XML处理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#通过例子来学习"><span class="toc-number">2.1.</span> <span class="toc-text">通过例子来学习</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#访问属性"><span class="toc-number">2.2.</span> <span class="toc-text">访问属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#探索DOM"><span class="toc-number">2.3.</span> <span class="toc-text">探索DOM</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用XPath表达式"><span class="toc-number">2.4.</span> <span class="toc-text">使用XPath表达式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#XML命名空间"><span class="toc-number">2.5.</span> <span class="toc-text">XML命名空间</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#转义"><span class="toc-number">2.6.</span> <span class="toc-text">转义</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#声明的XML处理"><span class="toc-number">3.</span> <span class="toc-text">声明的XML处理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#默认处理器"><span class="toc-number">3.1.</span> <span class="toc-text">默认处理器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#访问单独节点"><span class="toc-number">3.2.</span> <span class="toc-text">访问单独节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#XML_命名空间"><span class="toc-number">3.3.</span> <span class="toc-text">XML 命名空间</span></a></li></ol></li></ol>
		
		</div>
		
        <h2 id="揭示XML文档">揭示XML文档</h2><p>test.xml<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="title">book</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="title">title</span>&gt;</span>Test Book<span class="tag">&lt;/<span class="title">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="title">chapter</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="title">title</span>&gt;</span>Ch1<span class="tag">&lt;/<span class="title">title</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="title">para</span>&gt;</span>p1.1<span class="tag">&lt;/<span class="title">para</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="title">para</span>&gt;</span>p1.2<span class="tag">&lt;/<span class="title">para</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="title">para</span>&gt;</span>p1.3<span class="tag">&lt;/<span class="title">para</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="title">chapter</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="title">chapter</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="title">title</span>&gt;</span>Ch2<span class="tag">&lt;/<span class="title">title</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="title">para</span>&gt;</span>p2.1<span class="tag">&lt;/<span class="title">para</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="title">para</span>&gt;</span>p2.2<span class="tag">&lt;/<span class="title">para</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="title">chapter</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="title">book</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<ul>
<li>根节点是“文档”而不是book</li>
<li>如果 B 是 A 的直接后继，我们说 B 节点是 A 节点的 child 子节点，A节点是B节点的parent父节点。</li>
<li>元素，文本，注释，处理指令都是DOM树的节点</li>
</ul>
<a id="more"></a>
<h3 id="将XML放到数据模型中">将XML放到数据模型中</h3><figure class="highlight openscad"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Map root = new HashMap<span class="params">()</span>;</span><br><span class="line">root.put<span class="params">(<span class="string">"doc"</span>, freemarker.ext.dom.NodeModel.parse<span class="params">(new File<span class="params">(<span class="string">"test.xml"</span>)</span>)</span>)</span>;</span><br></pre></td></tr></table></figure>
<ul>
<li>parse 方法默认移除注释和处理指令节点。参见 API 文档获取详细信息。</li>
<li>NodeModel 也允许你直接包装 org.w3c.dom.Node。首先你也许想用静态的实用方法清空 DOM 树，比如 NodeModel.simplify 或你自定义的清空规则。</li>
</ul>
<h2 id="必要的XML处理">必要的XML处理</h2><h3 id="通过例子来学习">通过例子来学习</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="title">h1</span>&gt;</span>$&#123;doc.book.title&#125;<span class="tag">&lt;/<span class="title">h1</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>尝试访问有子元素的元素将导致错误<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="title">h1</span>&gt;</span>$&#123;doc.book&#125;<span class="tag">&lt;/<span class="title">h1</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>doc.book.chapter是存储两个元素节点的序列<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="title">h2</span>&gt;</span>$&#123;doc.book.chapter[0].title&#125;<span class="tag">&lt;/<span class="title">h2</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">h2</span>&gt;</span>$&#123;doc.book.chapter[1].title&#125;<span class="tag">&lt;/<span class="title">h2</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="title">#list</span> <span class="attribute">doc.book.chapter</span> <span class="attribute">as</span> <span class="attribute">ch</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="title">h2</span>&gt;</span>$&#123;ch.title&#125;<span class="tag">&lt;/<span class="title">h2</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="title">#list</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>就算只有一个元素节点，也可以当做一个序列<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="title">h1</span>&gt;</span>$&#123;doc.book[0].title[0]&#125;<span class="tag">&lt;/<span class="title">h1</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>如果 book 没有 chapter，那么book.chapter 就是一个空序列，所以 doc.book.chapter 就不会是 false，它就一直是 true！类似地，<code>&lt;#if doc.book.somethingTotallyNonsense??&gt;&lt;/#if&gt;</code>也不会是false。来检查是否有子节点，可以使用doc.book.chapter[0]??（或doc.book.chapter?size == 0）。当然你可以使用类似所有的控制处理操作符（比如 doc.book.author[0]!”Anonymous”），只是不要忘了那个[0]。</p>
<p>一个完整示例<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="title">#assign</span> <span class="attribute">book</span> = <span class="attribute">doc.book</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">h1</span>&gt;</span>$&#123;book.title&#125;<span class="tag">&lt;/<span class="title">h1</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">#list</span> <span class="attribute">book.chapter</span> <span class="attribute">as</span> <span class="attribute">ch</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="title">h2</span>&gt;</span>$&#123;ch.title&#125;<span class="tag">&lt;/<span class="title">h2</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="title">#list</span> <span class="attribute">ch.para</span> <span class="attribute">as</span> <span class="attribute">p</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="title">p</span>&gt;</span>$&#123;p&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="title">#list</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="title">#list</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>得到所有的para<br><figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;#<span class="keyword">list</span> doc.book.chapter.para <span class="keyword">as</span> p&gt;</span><br><span class="line">&lt;p&gt;<span class="label">$&#123;p&#125;</span></span><br><span class="line">&lt;/#<span class="keyword">list</span>&gt;</span><br></pre></td></tr></table></figure></p>
<h3 id="访问属性">访问属性</h3><p>如果test.xml中title是属性而不是元素，只需在其前加@即可。<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="title">#assign</span> <span class="attribute">book</span> = <span class="attribute">doc.book</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">h1</span>&gt;</span>$&#123;book.@title&#125;<span class="tag">&lt;/<span class="title">h1</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">#list</span> <span class="attribute">book.chapter</span> <span class="attribute">as</span> <span class="attribute">ch</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="title">h2</span>&gt;</span>$&#123;ch.@title&#125;<span class="tag">&lt;/<span class="title">h2</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="title">#list</span> <span class="attribute">ch.para</span> <span class="attribute">as</span> <span class="attribute">p</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="title">p</span>&gt;</span>$&#123;p&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="title">#list</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="title">#list</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>如果你很好奇是否 foo 含有属性 bar，那么你不得不写 foo.@bar[0]??来验证。（ foo.@bar??是不对的，因为它总是返回 true）。类似地，如果你想要一个bar属性的默认值，那么你就不得不写 foo.@bar[0]!”theDefaultValue”。</p>
<h3 id="探索DOM">探索DOM</h3><p>枚举所有book的子元素<br><figure class="highlight smalltalk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;<span class="symbol">#list</span> doc.book?children as c&gt;</span><br><span class="line">- <span class="char">$&#123;</span>c?node_type&#125; &lt;<span class="symbol">#if</span> c?node_type = <span class="string">'element'</span>&gt;<span class="char">$&#123;</span>c?node_name&#125;&lt;/<span class="symbol">#if</span>&gt;</span><br><span class="line">&lt;/<span class="symbol">#list</span>&gt;</span><br></pre></td></tr></table></figure></p>
<p>children：子元素序列<br>node_type：节点类型，”element”， “text”， “comment”， “pi”<br>node_name：节点名称</p>
<figure class="highlight nix"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;book <span class="variable">foo=</span><span class="string">"Foo"</span> <span class="variable">bar=</span><span class="string">"Bar"</span> <span class="variable">baaz=</span><span class="string">"Baaz"</span>&gt;</span><br></pre></td></tr></table></figure>
<p>可以通过元素的自变量@@获取元素的属性序列</p>
<figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;#<span class="keyword">list</span> doc.book.@@ <span class="keyword">as</span> attr&gt;</span><br><span class="line">- <span class="label">$&#123;attr</span>?node_name&#125; = <span class="label">$&#123;attr&#125;</span></span><br><span class="line">&lt;/#<span class="keyword">list</span>&gt;</span><br></pre></td></tr></table></figure>
<p>返回元素的子节点序列可以用*</p>
<figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;#<span class="keyword">list</span> doc.book.* <span class="keyword">as</span> c&gt;</span><br><span class="line">- <span class="label">$&#123;c</span>?node_name&#125;</span><br><span class="line">&lt;/#<span class="keyword">list</span>&gt;</span><br></pre></td></tr></table></figure>
<p>可以使用内建函数 parent 来获得元素的父节点。你可以使用内建函数 root 来快速返回到文档节点<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;<span class="comment">#assign e = doc.book.chapter[0].para[0]&gt;</span></span><br><span class="line">$&#123;e?node_name&#125;</span><br><span class="line">$&#123;e?<span class="keyword">parent</span>?node_name&#125;</span><br><span class="line">$&#123;e?<span class="keyword">parent</span>?<span class="keyword">parent</span>?node_name&#125;</span><br><span class="line">$&#123;e?<span class="keyword">parent</span>?<span class="keyword">parent</span>?<span class="keyword">parent</span>?node_name&#125;</span><br><span class="line">$&#123;e?root?node_name&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="使用XPath表达式">使用XPath表达式</h3><p>XPath 表达式仅在 Jaxen（推荐使用，但是使用至少 Jaxen 1.1-beta-8 版本，不能再老了） 或 Apache Xalan 库可用时有效。</p>
<figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml"><span class="tag">&lt;<span class="title">#list</span> <span class="attribute">doc</span>["<span class="attribute">book</span>/<span class="attribute">chapter</span>[<span class="attribute">title</span>=<span class="value">'Ch1'</span><span class="value">]</span>/<span class="attribute">para</span>"] <span class="attribute">as</span> <span class="attribute">p</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="title">p</span>&gt;</span></span>$&#123;p&#125;</span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="title">#list</span>&gt;</span></span></span><br><span class="line">$&#123;doc[<span class="link_label">"book/chapter[title='Ch1'</span>]/para[<span class="link_label">1</span>]"][0]&#125;</span><br><span class="line">$&#123;doc.book["chapter[title='Ch1']/para[1]"]&#125;</span><br></pre></td></tr></table></figure>
<p>注意 XPath 序列的项索引从 1 开始，而 FTL 的序列项索引是用 0 开始的。</p>
<p>如果使用 Jaxen 而不是 Xalan，那么 FreeMarker 的变量在使用 XPath 变量引用时是可见的<br><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;<span class="id">#assign</span> currentTitle = <span class="string">"Ch1"</span>&gt;</span><br><span class="line">&lt;<span class="id">#list</span> doc[<span class="string">"book/chapter[title=$currentTitle]/para"</span>] as p&gt;</span><br></pre></td></tr></table></figure></p>
<p>注意$currentTitle 不是 FreeMarker 的插值，因为那里没有{和}。那是 XPath 表达式。</p>
<p>一些 XPath 表达式的结果不是节点集，而是字符串，数字或者布尔值。获取para元素的总数<br><figure class="highlight elixir"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$&#123;</span>x[<span class="string">"count(//para)"</span>]&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="XML命名空间">XML命名空间</h3><p>如果元素book是命名空间 <a href="http://example.com/ebook，那么你不得不关联一个前缀，要在模板的顶部使用" target="_blank" rel="external">http://example.com/ebook，那么你不得不关联一个前缀，要在模板的顶部使用</a> ftl 指令的 the ns_prefixes 参数：</p>
<figure class="highlight 1c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;<span class="preprocessor">#ftl ns_prefixes=&#123;</span></span><br><span class="line"><span class="string">"e"</span>:<span class="string">"http://example.com/ebook"</span>,</span><br><span class="line"><span class="string">"f"</span>:<span class="string">"http://example.com/form"</span>,</span><br><span class="line"><span class="string">"vg"</span>:<span class="string">"http://example.com/vectorGraphics"</span>&#125;</span><br><span class="line">&gt;</span><br></pre></td></tr></table></figure>
<p>现在你可以编写如 doc[“e:book”]的表达式。使用保留前缀D可以设置默认命名空间。XPath不支持默认命名空间</p>
<figure class="highlight dust"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml"><span class="tag">&lt;<span class="title">#ftl</span> <span class="attribute">ns_prefixes</span>=</span></span><span class="expression">&#123;<span class="string">"D"</span>:<span class="string">"http://example.com/ebook"</span>&#125;</span><span class="xml"><span class="tag">&gt;</span></span></span><br></pre></td></tr></table></figure>
<p>注意当你使用默认命名空间时，那么你可以使用保留前缀 N 来选择不属于任意节点空间的元素。比如 doc.book[“N:foo”]。这对 XPath 表达式不起作用</p>
<h3 id="转义">转义</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="title">#escape</span> <span class="attribute">x</span> <span class="attribute">as</span> <span class="attribute">x</span>?<span class="attribute">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">#assign</span> <span class="attribute">book</span> = <span class="attribute">doc.book</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">h1</span>&gt;</span>$&#123;book.title&#125;<span class="tag">&lt;/<span class="title">h1</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">#list</span> <span class="attribute">book.chapter</span> <span class="attribute">as</span> <span class="attribute">ch</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">h2</span>&gt;</span>$&#123;ch.title&#125;<span class="tag">&lt;/<span class="title">h2</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">#list</span> <span class="attribute">ch.para</span> <span class="attribute">as</span> <span class="attribute">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">p</span>&gt;</span>$&#123;p&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="title">#list</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="title">#list</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="title">#escape</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="声明的XML处理">声明的XML处理</h2><p>最经常使用来处理声明方式的指令就是 recurse 指令，这个指令获取节点变量，并把它作为是参数，从第一个子元素开始，一个接一个地“访问”所有它的子元素。“访问”一个节点意味着它调用了用户自定义的指令（比如宏），它的名字和子节点（ ?node_name）的名字相同。我们这么说，用户自定义指令操作节点。使用用户自定义指令处理的节点作为特殊变量.node 是可用的。</p>
<figure class="highlight livecodeserver"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&lt;<span class="comment">#recurse doc&gt;</span></span><br><span class="line">&lt;<span class="comment">#macro book&gt;</span></span><br><span class="line">    Book <span class="keyword">element</span> <span class="operator">with</span> title $&#123;.node.title&#125;</span><br><span class="line">    &lt;<span class="comment">#recurse&gt;</span></span><br><span class="line">    End book</span><br><span class="line">&lt;/<span class="comment">#macro&gt;</span></span><br><span class="line">&lt;<span class="comment">#macro title&gt;</span></span><br><span class="line">    Title <span class="keyword">element</span></span><br><span class="line">&lt;/<span class="comment">#macro&gt;</span></span><br><span class="line">&lt;<span class="comment">#macro chapter&gt;</span></span><br><span class="line">    Chapter <span class="keyword">element</span> <span class="operator">with</span> title: $&#123;.node.title&#125;</span><br><span class="line">&lt;/<span class="comment">#macro&gt;</span></span><br></pre></td></tr></table></figure>
<p>如果你调用 recurse 而不用参数，那么它使用.node，也就是说，它访问现在处理这个节点的所有子节点。</p>
<p>所有文本节点的节点名字都是@text。转义HTML<br><figure class="highlight d"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;#<span class="keyword">macro</span> <span class="keyword">@text</span>&gt;$&#123;.node?html&#125;&lt;/#<span class="keyword">macro</span>&gt;</span><br></pre></td></tr></table></figure></p>
<h3 id="默认处理器">默认处理器</h3><ul>
<li>文本节点： 打印其中的文本。要注意，在很多应用程序中，这对你来说并不好，因为你应该在你发送它们到输出（使用?html 或?xml 或?rtf 等，这基于输出的格式）前转义这些文本。</li>
<li>处理指令节点： 如果你定义了自定义指令，可以通过调用处理器调用@pi，否则将什么都不做（忽略这些节点）。</li>
<li>注释节点，文档类型节点：什么都不做（忽略这些节点）。</li>
<li>文档节点：调用 recurse，也就是说，访问文档节点的所有子节点。</li>
</ul>
<p>元素节点的情形，这意味着如果你定义了一个称为@element 的宏（或其他种类的用户自定义指令），没有其他特定的处理器时，那么它会捕捉所有元素节点。如果你没有@element 处理器，那么你必须为所有可能的元素定义处理器。</p>
<p>属性节点在 recurse 指令中不可见，所以不需要为它们编写处理器。</p>
<h3 id="访问单独节点">访问单独节点</h3><p>使用visit指令你可以访问单独的节点</p>
<h3 id="XML_命名空间">XML 命名空间</h3><figure class="highlight armasm"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;<span class="keyword">book </span>xmlns=<span class="string">"http://example.com/ebook"</span>&gt;</span><br></pre></td></tr></table></figure>
<p>考虑命名空间</p>
<figure class="highlight dust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml"><span class="tag">&lt;<span class="title">#ftl</span> <span class="attribute">ns_prefixes</span>=</span></span><span class="expression">&#123;<span class="string">"e"</span>:<span class="string">"http://example.com/ebook"</span>&#125;</span><span class="xml"><span class="tag">&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">#recurse</span> <span class="attribute">doc</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">#macro</span> "<span class="attribute">e:book</span>"&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">title</span>&gt;</span><span class="tag">&lt;<span class="title">#recurse</span> <span class="attribute">.node</span>["<span class="attribute">e:title</span>"]&gt;</span><span class="tag">&lt;/<span class="title">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="title">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">h1</span>&gt;</span><span class="tag">&lt;<span class="title">#recurse</span> <span class="attribute">.node</span>["<span class="attribute">e:title</span>"]&gt;</span><span class="tag">&lt;/<span class="title">h1</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">#recurse</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="title">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="title">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="title">#macro</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">#macro</span> "<span class="attribute">e:chapter</span>"&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">h2</span>&gt;</span><span class="tag">&lt;<span class="title">#recurse</span> <span class="attribute">.node</span>["<span class="attribute">e:title</span>"]&gt;</span><span class="tag">&lt;/<span class="title">h2</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">#recurse</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="title">#macro</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">#macro</span> "<span class="attribute">e:para</span>"&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">p</span>&gt;</span><span class="tag">&lt;<span class="title">#recurse</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="title">#macro</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">#macro</span> "<span class="attribute">e:title</span>"&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">#--</span></span><br><span class="line"><span class="attribute">We</span> <span class="attribute">have</span> <span class="attribute">handled</span> <span class="attribute">this</span> <span class="attribute">element</span> <span class="attribute">imperatively</span>,</span><br><span class="line"><span class="attribute">so</span> <span class="attribute">we</span> <span class="attribute">do</span> <span class="attribute">nothing</span> <span class="attribute">here.</span></span><br><span class="line"><span class="attribute">--</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="title">#macro</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="title">#macro</span> @<span class="attribute">text</span>&gt;</span>$</span><span class="expression">&#123;<span class="variable">.node</span>?<span class="variable">html</span>&#125;</span><span class="xml"><span class="tag">&lt;/<span class="title">#macro</span>&gt;</span></span></span><br></pre></td></tr></table></figure>
<p>设置为默认命名空间</p>
<figure class="highlight lasso"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="subst">&lt;</span><span class="variable">#ftl</span> ns_prefixes<span class="subst">=</span>&#123;<span class="string">"D"</span>:<span class="string">"http://example.com/ebook"</span>&#125;<span class="subst">&gt;</span></span><br><span class="line"><span class="subst">&lt;</span><span class="variable">#recurse</span> doc<span class="subst">&gt;</span></span><br><span class="line"><span class="subst">&lt;</span><span class="variable">#macro</span> book<span class="subst">&gt;</span></span><br><span class="line"><span class="attribute">...</span></span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
	  
	  <!-- 百度分享 Start -->
	  <div class="bdsharebuttonbox"><a href="#" class="bds_more" data-cmd="more"></a><a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间"></a><a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a><a href="#" class="bds_tqq" data-cmd="tqq" title="分享到腾讯微博"></a><a href="#" class="bds_renren" data-cmd="renren" title="分享到人人网"></a><a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信"></a></div>
	  <!-- 百度分享 End -->
	  
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/FreeMarker/">FreeMarker</a></li></ul>

	  
<span>
Updated:<time datetime="2015-05-21T15:55:14.000Z" itemprop="dateModified">2015-05-21</time>
</span>


    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2015/05/12/shiro-note/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Shiro笔记
        
      </div>
    </a>
  
  
    <a href="/2015/05/02/freemarker-program-development-note/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">FreeMarker-程序开发指南笔记</div>
    </a>
  
</nav>

  
</article>



<!-- 多说评论框 start -->

<section id="comments">
  <div class="ds-thread"  data-thread-key="/2015/05/03/freemarker-XML-processing-note/" data-title="FreeMarker-XML处理笔记" data-url="http://howiefh.github.io/2015/05/03/freemarker-XML-processing-note/" id="ds_thread">
    <noscript>Please enable JavaScript to view the <a href="//duoshuo.com/?ref_noscript">comments powered by duoshuo.</a></noscript>
  </div>
</section>

<!-- 多说评论框 end -->
</section>
        
          
  <div id="toc" class="toc-aside">
  <h2 class="toc-title">Contents</h2>
    
        <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#揭示XML文档"><span class="toc-number">1.</span> <span class="toc-text">揭示XML文档</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#将XML放到数据模型中"><span class="toc-number">1.1.</span> <span class="toc-text">将XML放到数据模型中</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#必要的XML处理"><span class="toc-number">2.</span> <span class="toc-text">必要的XML处理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#通过例子来学习"><span class="toc-number">2.1.</span> <span class="toc-text">通过例子来学习</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#访问属性"><span class="toc-number">2.2.</span> <span class="toc-text">访问属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#探索DOM"><span class="toc-number">2.3.</span> <span class="toc-text">探索DOM</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用XPath表达式"><span class="toc-number">2.4.</span> <span class="toc-text">使用XPath表达式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#XML命名空间"><span class="toc-number">2.5.</span> <span class="toc-text">XML命名空间</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#转义"><span class="toc-number">2.6.</span> <span class="toc-text">转义</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#声明的XML处理"><span class="toc-number">3.</span> <span class="toc-text">声明的XML处理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#默认处理器"><span class="toc-number">3.1.</span> <span class="toc-text">默认处理器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#访问单独节点"><span class="toc-number">3.2.</span> <span class="toc-text">访问单独节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#XML_命名空间"><span class="toc-number">3.3.</span> <span class="toc-text">XML 命名空间</span></a></li></ol></li></ol>
    
  </div>

<aside id="sidebar">

  
    
<div class="widget-wrap">
  <h3 class="widget-title">ABOUT ME</h3>
  <ul class="widget about-me">
    
    <li><img class="author" title="About me" src="http://fh-1.qiniudn.com/okal-eltocat.jpg" /></li>
    
    
    <li>Hi,I'm FengHao.</li>
    
    <li>I'll share something interesting and my learning experience with you at this blog.</li>
    
    <li>前博客:<a href="http://hi.baidu.com/idea_star" target="_BLANK">百度空间</a></li>
    
  </ul>
</div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Android/">Android</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/C/">C</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Database/">Database</a><span class="category-list-count">13</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/Database/MongoDB/">MongoDB</a><span class="category-list-count">10</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Database/MySQL/">MySQL</a><span class="category-list-count">2</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/Eclipse/">Eclipse</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/FTP/">FTP</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Git/">Git</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Hexo/">Hexo</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Java/">Java</a><span class="category-list-count">20</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/Java/FreeMarker/">FreeMarker</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Java/Shiro/">Shiro</a><span class="category-list-count">2</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/JavaEE/">JavaEE</a><span class="category-list-count">4</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/JavaEE/Hibernate/">Hibernate</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/JavaEE/JSP/">JSP</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/JavaEE/Spring/">Spring</a><span class="category-list-count">2</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/JavaScript/">JavaScript</a><span class="category-list-count">5</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/JavaScript/jQuery/">jQuery</a><span class="category-list-count">1</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/Linux/">Linux</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/PHP/">PHP</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Suse/">Suse</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Ubuntu/">Ubuntu</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Vim/">Vim</a><span class="category-list-count">8</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/编程/">编程</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/软件/">软件</a><span class="category-list-count">1</span></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Android/" style="font-size: 14.75px; color: #65bfa7">Android</a> <a href="/tags/C/" style="font-size: 13.88px; color: #71c1c2">C</a> <a href="/tags/CAS/" style="font-size: 13px; color: #7dc3de">CAS</a> <a href="/tags/Chrome/" style="font-size: 13px; color: #7dc3de">Chrome</a> <a href="/tags/Clean-Code/" style="font-size: 13.88px; color: #71c1c2">Clean Code</a> <a href="/tags/Database/" style="font-size: 13px; color: #7dc3de">Database</a> <a href="/tags/Eclipse/" style="font-size: 13px; color: #7dc3de">Eclipse</a> <a href="/tags/FTP/" style="font-size: 13.88px; color: #71c1c2">FTP</a> <a href="/tags/FreeMarker/" style="font-size: 14.75px; color: #65bfa7">FreeMarker</a> <a href="/tags/Gcc/" style="font-size: 13px; color: #7dc3de">Gcc</a> <a href="/tags/Git/" style="font-size: 14.75px; color: #65bfa7">Git</a> <a href="/tags/Github-Pages/" style="font-size: 13px; color: #7dc3de">Github Pages</a> <a href="/tags/Hexo/" style="font-size: 14.75px; color: #65bfa7">Hexo</a> <a href="/tags/Hibernate/" style="font-size: 13px; color: #7dc3de">Hibernate</a> <a href="/tags/JSP/" style="font-size: 13px; color: #7dc3de">JSP</a> <a href="/tags/JVM/" style="font-size: 14.75px; color: #65bfa7">JVM</a> <a href="/tags/Java/" style="font-size: 14.75px; color: #65bfa7">Java</a> <a href="/tags/JavaMail/" style="font-size: 13px; color: #7dc3de">JavaMail</a> <a href="/tags/JavaScript/" style="font-size: 16.5px; color: #4dbc6f">JavaScript</a> <a href="/tags/Linux/" style="font-size: 13.88px; color: #71c1c2">Linux</a> <a href="/tags/Log/" style="font-size: 13px; color: #7dc3de">Log</a> <a href="/tags/Markdown/" style="font-size: 13.88px; color: #71c1c2">Markdown</a> <a href="/tags/MongoDB/" style="font-size: 20px; color: #1db400">MongoDB</a> <a href="/tags/MySQL/" style="font-size: 13.88px; color: #71c1c2">MySQL</a> <a href="/tags/PHP/" style="font-size: 16.5px; color: #4dbc6f">PHP</a> <a href="/tags/Rhythmbox/" style="font-size: 13px; color: #7dc3de">Rhythmbox</a> <a href="/tags/SSO/" style="font-size: 13px; color: #7dc3de">SSO</a> <a href="/tags/Servlet/" style="font-size: 13px; color: #7dc3de">Servlet</a> <a href="/tags/Shiro/" style="font-size: 13.88px; color: #71c1c2">Shiro</a> <a href="/tags/Spring/" style="font-size: 13.88px; color: #71c1c2">Spring</a> <a href="/tags/Suse/" style="font-size: 13px; color: #7dc3de">Suse</a> <a href="/tags/Thinking-in-Java/" style="font-size: 19.13px; color: #29b61c">Thinking in Java</a> <a href="/tags/Ubuntu/" style="font-size: 17.38px; color: #41ba53">Ubuntu</a> <a href="/tags/Vim/" style="font-size: 18.25px; color: #35b838">Vim</a> <a href="/tags/VirtualBox/" style="font-size: 13px; color: #7dc3de">VirtualBox</a> <a href="/tags/Vsftpd/" style="font-size: 13px; color: #7dc3de">Vsftpd</a> <a href="/tags/jQuery/" style="font-size: 13px; color: #7dc3de">jQuery</a> <a href="/tags/pam-mysql/" style="font-size: 13px; color: #7dc3de">pam_mysql</a> <a href="/tags/小米/" style="font-size: 13px; color: #7dc3de">小米</a> <a href="/tags/软件/" style="font-size: 15.63px; color: #59bd8b">软件</a>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/09/">September 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/08/">August 2015</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/07/">July 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/05/">May 2015</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/04/">April 2015</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/03/">March 2015</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/02/">February 2015</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/12/">December 2014</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/11/">November 2014</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/10/">October 2014</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/09/">September 2014</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/08/">August 2014</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/07/">July 2014</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/06/">June 2014</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/05/">May 2014</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/04/">April 2014</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/03/">March 2014</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/02/">February 2014</a><span class="archive-list-count">11</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/01/">January 2014</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/12/">December 2013</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/11/">November 2013</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/08/">August 2013</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/07/">July 2013</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/06/">June 2013</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/05/">May 2013</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/04/">April 2013</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2012/03/">March 2012</a><span class="archive-list-count">1</span></li></ul>
    </div>
  </div>


  
    <div class="widget-wrap">
  <h3 class="widget-title">Calendar</h3>
  <div class="widget">
    <div id="g-calendar" class="g-calendar">
        <span class="g-calendar-prev"></span>
		 
        <span class="g-calendar-back"></span>
        <span class="g-calendar-now"></span>
		 
        <span class="g-calendar-next"></span>
        <div class="g-calendar-hd"></div>
        <div class="g-calendar-tit"></div>
        <div class="g-calendar-bd"></div>
    </div>
  </div>
</div>
<script type="text/javascript">
function LGY_calendar(wrapId, options){
    this.oWrap = this.getId(wrapId);
    this.oHead = this.getByClassName('g-calendar-hd',this.oWrap)[0];
    this.oBody = this.getByClassName('g-calendar-bd',this.oWrap)[0];
    this.oTit = this.getByClassName('g-calendar-tit',this.oWrap)[0];
    this.oPrev = this.getByClassName('g-calendar-prev',this.oWrap)[0];
    this.oNext = this.getByClassName('g-calendar-next',this.oWrap)[0];
    this.oNow = this.getByClassName('g-calendar-now',this.oWrap)[0];
    this.oBack = this.getByClassName('g-calendar-back',this.oWrap)[0];
    this.init();
}
LGY_calendar.prototype = {
    ///////////获取ID元素
    getId:function(id){
        return document.getElementById(id);
    },
    ////////获取css类名元素
    getByClassName:function(className,parent){
        var elem = [],
            node = parent != undefined&&parent.nodeType==1?parent.getElementsByTagName('*'):document.getElementsByTagName('*'),
            p = new RegExp("(^|\\s)"+className+"(\\s|$)");
        for(var n=0,i=node.length;n<i;n++){
            if(p.test(node[n].className)){
                elem.push(node[n]);
            }
        }
        return elem;
    },
    //填充日历
    fillDate:function(year,month){
        //本月份第一天是星期几-为显示上个月的天数做铺垫
        var first_day = new Date(year,month,1).getDay(),
        //如果刚好是星期天，则空出一行（显示上个月的天数）
            first_day = first_day == 0?first_day=7:first_day;
        //本月份最后一天是几号
        var final_date = new Date(year,month+1,0).getDate(),
        //上个月的最后一天是几号
            last_date = new Date(year,month,0).getDate(),
        //剩余的格子数--即排在末尾的格子数
            surplus = 42 - first_day - final_date;
        //设置年的链接
        var yearHead = "<a href='/" + "archives/" + year + "/'>" + year + " "+ "</a>"; 
        //设置年的链接
        var monthHead = "";
        var month1 = month + 1;
        if (month1 < 10) {
            monthHead = "<a href='/" + "archives/" + year + "/" + "0" + month1 + "/'>" + " " + month1 + " " + "</a>";
        } else {
            monthHead = "<a href='/" + "archives/" + year + "/" + month1 + "/'>" + " " + month1 + " " + "</a>";
        }
        //设置表头的日历
        this.oHead.innerHTML = yearHead+'年'+monthHead+'月';
        //填充日历执行
        var html = '';
        //上个月的显示天数
        for(var i=0;i<first_day;i++){
            html+='<span class="g-calendar-grey">'+(last_date-(first_day-1)+i)+'</span>';
        }
        //本月的显示天数
        var postdate = new Date("Sun May 03 2015 09:20:51 GMT+0800"); 
        if (true && postdate.getFullYear() == year && postdate.getMonth() == month) { 
            html += '<span>1</span><span><a href="/2015/05/02/freemarker-program-development-note/" title="FreeMarker-程序开发指南笔记">2</a></span><span><a href="/2015/05/03/freemarker-XML-processing-note/" title="FreeMarker-XML处理笔记">3</a></span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span><a href="/2015/05/12/shiro-note/" title="Shiro笔记">12</a></span><span>13</span><span>14</span><span>15</span><span>16</span><span>17</span><span>18</span><span><a href="/2015/05/19/shiro-cas-single-sign-on/" title="Shiro & CAS 实现单点登录">19</a></span><span>20</span><span>21</span><span><a href="/2015/05/22/vim-install-youcompleteme-plugin/" title="Vim 自动补全插件 YouCompleteMe 安装与配置">22</a></span><span>23</span><span>24</span><span>25</span><span>26</span><span>27</span><span>28</span><span>29</span><span>30</span>';
        } else {
            for(var j=0;j<final_date;j++){        
                html+='<span id="d'+(j+1)+'">'+(j+1)+'</span>';
            }
        }
        //下个月的显示天数
        for(var k=0;k<surplus;k++){
            html+='<span class="g-calendar-grey">'+(k+1)+'</span>';
        }
        //fill
        this.oBody.innerHTML = html;
        // 当前状态
        if(year==this.c_year&&this.c_month==month){
            this.oBody.getElementsByTagName('span')[first_day+this.date-1].className='g-calendar-on';
        }

        // 对所有文章遍历,然后将有文章的日期加上链接,如果文章太多的话,生成页面会很大,去掉了
        
    },
    // next切换
    next:function(){
        var _that = this;
        this.oNext.onclick = function(){
            _that.month++;
            if(_that.month>11){
                _that.month = 0;
                _that.year++;
            }
            // 填充日历
            _that.fillDate(_that.year,_that.month);
        };
    },
    // back 切换
    back:function(){
        var _that = this;
        if(this.oBack != undefined) {
            this.oBack.onclick = function(){
                var postdate = new Date("Sun May 03 2015 09:20:51 GMT+0800"); 
                _that.year = postdate.getFullYear();
                _that.month = postdate.getMonth();
                // 填充日历
                _that.fillDate(_that.year,_that.month);
            };
        }
    },
    // now切换
    now:function(){
        var _that = this;
        if(this.oNow != undefined ) {
            this.oNow.onclick = function(){
                var nowDate = new Date(); 
                _that.year = nowDate.getFullYear();
                _that.month = nowDate.getMonth();
                // 填充日历
                _that.fillDate(_that.year,_that.month);
            };
        }
    },
    // prev切换
    prev:function(){
        var _that = this;
        this.oPrev.onclick = function(){
            _that.month--;
            if(_that.month<0){
                _that.month = 11;
                _that.year--;
            }
            // 填充日历
            _that.fillDate(_that.year,_that.month);
        };
    },
    init:function(){
        this.oTit.innerHTML = '<span>日</span><span>一</span><span>二</span><span>三</span><span>四</span><span>五</span><span>六</span>';
        // 获取今天的日历时间
        var now = new Date();
        this.c_year = this.year = now.getFullYear();
        this.c_month = this.month = now.getMonth();
        this.date = now.getDate();
        // 初始化--填充日历
        this.fillDate(this.year,this.month);
        //next切换
        this.next();
        //prev切换
        this.prev();
        //back 切换
        this.back();
        //now 切换
        this.now();
    }
}
new LGY_calendar('g-calendar');
</script>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2015/09/01/javascript-summary/">JavaScript 总结</a>
          </li>
        
          <li>
            <a href="/2015/08/28/javascript-oop-function-expression-and-async/">JavaScript 面向对象程序设计、函数表达式和异步编程</a>
          </li>
        
          <li>
            <a href="/2015/08/28/javascript-reference-type/">JavaScript 引用类型</a>
          </li>
        
          <li>
            <a href="/2015/08/28/javascript-grammar/">JavaScript 基本语法</a>
          </li>
        
          <li>
            <a href="/2015/08/10/java-web/">Java Web 笔记</a>
          </li>
        
      </ul>
    </div>
  </div>


  
    
<div class="widget-wrap">
  <h3 class="widget-title">Recent Comments</h3>
  <ul class="widget ds-recent-comments" data-num-items="5" data-show-avatars="0" data-show-title="1" data-show-time="1"></ul>
</div>
<!-- 需要多说的公用代码 -->


  

</aside>

        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2015 howiefh<br>
      Powered by <a href="http://zespia.tw/hexo/" target="_blank">Hexo</a> and Theme by <a href="https://github.com/howiefh/hexo-theme-landscape-f" target="_blank" title="Landscape-F">Landscape-F</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<!-- 多说公共JS代码 start (一个网页只需插入一次) -->

<script type="text/javascript">
  var duoshuoQuery = {short_name:"howiefh"};
  (function() {
	var ds = document.createElement('script');
	ds.type = 'text/javascript';ds.async = true;
	ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
	ds.charset = 'UTF-8';
	(document.getElementsByTagName('head')[0] 
		|| document.getElementsByTagName('body')[0]).appendChild(ds);
  })();
</script> 

<!-- 多说公共JS代码 end -->

<!-- 百度分享 start -->

<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":["mshare","douban","bdysc","sqq","qq","hi","baidu","huaban","youdao","sdo","mail","xg","diandian","fx","copy","print"],"bdPic":"","bdStyle":"1","bdSize":"16"},"share":{},"image":{"viewList":["qzone","tsina","tqq","renren","weixin"],"viewText":"分享到：","viewSize":"16"}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>

<!-- 百度分享 end -->

<!--
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="/js/jquery.min.js"></script>
-->
<script src="/js/jquery.min.js" type="text/javascript"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css" type="text/css">
  <script src="/fancybox/jquery.fancybox.pack.js" type="text/javascript"></script>


<div class="bottom-btn">

	<a class="icon-gotop" href="javascript:void(0)" title="返回顶部"></a>
	<script src="/js/gotop.js" type="text/javascript"></script>
	<!--
	<script src="/js/gotop.js"></script>
	-->


	<a class="icon-toc-toggle" href="javascript:void(0)" title="文章目录"></a>
	<!--
	<script src="/js/toc_aside_toggle.js"></script>
	-->

</div>
<script src="/js/toc_aside_toggle.js" type="text/javascript"></script>


<script src="/js/script.js" type="text/javascript"></script>

  </div>
</body>
</html>
